package com.ying.dao.system;

import com.ying.po.order.Orange;
import com.ying.po.system.Permission;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import javax.transaction.Transactional;
import java.util.List;

public interface PermissionRepository extends JpaRepository<Permission, Integer> {

    @Query(value = "select permission_id from t_user2permission where user_type_id=?1", nativeQuery = true)
    List<Integer> findPermissionsByUserType(Integer id);

    @Query(value = "select * from t_permission", nativeQuery = true)
    public Page<Permission> getPermissionPage(Pageable pageable);

    @Query(value = "select permission_id from t_user2permission where user_type_id=?1", nativeQuery = true)
    List<Integer> findPermissionIdByRoleCode(Integer roleCode);

    @Modifying
    @Transactional
    @Query(value = "insert into t_user2permission (user_type_id, permission_id) value (:co, :id)", nativeQuery = true)
    void createNewPerIdToRole(@Param("id") Integer id, @Param("co") Integer roleCode);

    @Modifying
    @Transactional
    @Query(value = "delete from t_user2permission where user_type_id=:co and permission_id=:id", nativeQuery = true)
    void deleteOldPerIdToRole(@Param("id") Integer id, @Param("co") Integer roleCode);
}
